﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace DLC.Module.DataTable.ArchiveBytes
{
    public static partial class DataTableBytesExtension
    {
        [BinaryWriterHelper(typeof(Vector2))]
        public static void WriteVector2(BinaryWriter writer, object data)
        {
            try
            {
                Vector2 value = (Vector2)data;
                writer.Write(value.x);
                writer.Write(value.y);
            }
            catch(Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector2[]))]
        public static void WriteVector2Array(BinaryWriter writer, object data)
        {
            try
            {
                Vector2[] value = (Vector2[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val.x);
                    writer.Write(val.y);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector2Int))]
        public static void WriteVector2Int(BinaryWriter writer, object data)
        {
            try
            {
                Vector2Int value = (Vector2Int)data;
                writer.Write(value.x);
                writer.Write(value.y);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector2Int[]))]
        public static void WriteVector2IntArray(BinaryWriter writer, object data)
        {
            try
            {
                Vector2Int[] value = (Vector2Int[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val.x);
                    writer.Write(val.y);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector3))]
        public static void WriteVector3(BinaryWriter writer, object data)
        {
            try
            {
                Vector3 value = (Vector3)data;
                writer.Write(value.x);
                writer.Write(value.y);
                writer.Write(value.z);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector3[]))]
        public static void WriteVector3Array(BinaryWriter writer, object data)
        {
            try
            {
                Vector3[] values = (Vector3[])data;
                writer.Write(values.Length);
                foreach (var value in values)
                {
                    writer.Write(value.x);
                    writer.Write(value.y);
                    writer.Write(value.z);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector3Int))]
        public static void WriteVector3Int(BinaryWriter writer, object data)
        {
            try
            {
                Vector3Int value = (Vector3Int)data;
                writer.Write(value.x);
                writer.Write(value.y);
                writer.Write(value.z);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector3Int[]))]
        public static void WriteVector3IntArray(BinaryWriter writer, object data)
        {
            try
            {
                Vector3Int[] values = (Vector3Int[])data;
                writer.Write(values.Length);
                foreach (var value in values)
                {
                    writer.Write(value.x);
                    writer.Write(value.y);
                    writer.Write(value.z);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector4))]
        public static void WriteVector4(BinaryWriter writer, object data)
        {
            try
            {
                Vector4 value = (Vector4)data;
                writer.Write(value.x);
                writer.Write(value.y);
                writer.Write(value.z);
                writer.Write(value.w);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(Vector4[]))]
        public static void WriteVector4Array(BinaryWriter writer, object data)
        {
            try
            {
                Vector4[] values = (Vector4[])data;
                writer.Write(values.Length);
                foreach (var value in values)
                {
                    writer.Write(value.x);
                    writer.Write(value.y);
                    writer.Write(value.z);
                    writer.Write(value.w);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }
    }
}